<% fields_index = Templates.build_field_areas_index(@submitter.submission.template_fields || @submitter.submission.template.fields) %>
<% values = @submitter.submission.submitters.reduce({}) { |acc, sub| acc.merge(sub.values) } %>
<% attachments_index = ActiveStorage::Attachment.where(record: @submitter.submission.submitters, name: :attachments).preload(:blob).index_by(&:uuid) %>
<% page_blob_struct = Struct.new(:url, :metadata, keyword_init: true) %>
<div style="max-height: -webkit-fill-available;">
  <div id="scrollbox">
    <div class="mx-auto block pb-72" style="max-width: 1000px">
      <%# flex block w-full sticky top-0 z-50 space-x-2 items-center bg-yellow-100 p-2 border-y border-yellow-200 %>
      <%= render 'banner' %>
      <% (@submitter.submission.template_schema || @submitter.submission.template.schema).each do |item| %>
        <% document = @submitter.submission.template_schema_documents.find { |a| a.uuid == item['attachment_uuid'] } %>
        <% document_annots_index = document.metadata.dig('pdf', 'annotations')&.group_by { |e| e['page'] } || {} %>
        <% preview_images_index = document.preview_images.loaded? ? document.preview_images.index_by { |e| e.filename.base.to_i } : {} %>
        <% lazyload_metadata = document.preview_images.last.metadata %>
        <% (document.metadata.dig('pdf', 'number_of_pages') || (document.preview_images.loaded? ? preview_images_index.size : document.preview_images.size)).times do |index| %>
          <% page = preview_images_index[index] || page_blob_struct.new(metadata: lazyload_metadata, url: preview_document_page_path(document.uuid, "#{index}.jpg")) %>
          <div class="relative my-4 shadow-md">
            <img loading="lazy" src="<%= page.url %>" width="<%= page.metadata['width'] %>" height="<%= page.metadata['height'] %>">
            <div id="page-<%= [document.uuid, index].join('-') %>" class="top-0 bottom-0 left-0 right-0 absolute">
              <% document_annots_index[index]&.each do |annot| %>
                <%= render 'submissions/annotation', annot: %>
              <% end %>
              <% fields_index.dig(document.uuid, index)&.each do |(area, field)| %>
                <% value = values[field['uuid']].presence || (field['readonly'] ? Submitters::SubmitValues.template_default_value_for_submitter(field['default_value'], @submitter.submission.submitters.find { |e| e.uuid == field['submitter_uuid'] }, with_time: false) : nil) %>
                <% next if value.blank? %>
                <% next if !field['readonly'] && field['submitter_uuid'] == @submitter.uuid %>
                <%= render 'submissions/value', area:, field:, attachments_index:, value:, locale: @submitter.submission.template.account.locale %>
              <% end %>
            </div>
          </div>
        <% end %>
      <% end %>
      <%= render 'shared/attribution', link_path: '/start' %>
    </div>
  </div>
</div>
<div class="fixed bottom-0 w-full h-0 z-20">
  <div class="mx-auto" style="max-width: 1000px">
    <div class="relative md:mx-32">
      <%= render 'submission_form', attachments_index:, submitter: @submitter %>
    </div>
  </div>
</div>
<%= render 'scripts/autosize_field' %>
